package com.plusjun.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plusjun.common.to.MemberPrice;
import com.plusjun.common.to.SkuReductionTO;
import com.plusjun.common.utils.PageUtils;
import com.plusjun.common.utils.Query;
import com.plusjun.gulimall.coupon.dao.SkuFullReductionDao;
import com.plusjun.gulimall.coupon.entity.MemberPriceEntity;
import com.plusjun.gulimall.coupon.entity.SkuFullReductionEntity;
import com.plusjun.gulimall.coupon.entity.SkuLadderEntity;
import com.plusjun.gulimall.coupon.service.MemberPriceService;
import com.plusjun.gulimall.coupon.service.SkuFullReductionService;
import com.plusjun.gulimall.coupon.service.SkuLadderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuFullReductionEntity> wrapper = new QueryWrapper<>();
        String sessionId = (String) params.get("promotionSessionId");
        if (StringUtils.isNotEmpty(sessionId)) {
            wrapper.eq("promotion_session_id",sessionId);
        }
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTO skuReductionTO) {
        // 1、保存满减打折信息，会员价
        // gulimall_sms -> sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        Long skuId = skuReductionTO.getSkuId();
        skuLadderEntity.setSkuId(skuId);
        int fullCount = skuReductionTO.getFullCount();
        skuLadderEntity.setFullCount(fullCount);
        BigDecimal discount = skuReductionTO.getDiscount();
        skuLadderEntity.setDiscount(discount);
        int countStatus = skuReductionTO.getCountStatus();
        skuLadderEntity.setAddOther(countStatus);
        if (fullCount > 0) {
            skuLadderService.save(skuLadderEntity);
        }

        // 2、保存满减信息
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTO, skuFullReductionEntity);
        if (discount.compareTo(BigDecimal.ZERO) == 1) {
            this.save(skuFullReductionEntity);
        }

        // 3、会员价格
        List<MemberPrice> memberPrice = skuReductionTO.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuId);
            Long levelId = item.getId();
            memberPriceEntity.setMemberLevelId(levelId);
            String levelName = item.getName();
            memberPriceEntity.setMemberLevelName(levelName);
            BigDecimal price = item.getPrice();
            memberPriceEntity.setMemberPrice(price);
            Integer addOther = 1;
            memberPriceEntity.setAddOther(addOther);
            return memberPriceEntity;
        }).filter(item -> {
            return item.getMemberPrice().compareTo(BigDecimal.ZERO) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}